package com.moli.lumpSugar.user.service.adapter;

import com.moli.lumpSugar.common.domain.enums.YesOrNoEnum;
import com.moli.lumpSugar.user.domain.entity.ItemConfig;
import com.moli.lumpSugar.user.domain.entity.User;
import com.moli.lumpSugar.user.domain.entity.UserBackpack;
import com.moli.lumpSugar.user.domain.vo.resp.BadgeResp;
import com.moli.lumpSugar.user.domain.vo.resp.UserInfoResp;

import org.springframework.beans.BeanUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class UserAdapter {
    public static User  buildUserSave(String openId)
    {
        return User.builder().openId(openId).build();
    }

//    public static User buildAuthorizeUser(Long uid,WxOAuth2UserInfo wxUserInfo) {
//        User user = new User();
//        user.setName(wxUserInfo.getNickname());
//        user.setAvatar(wxUserInfo.getHeadImgUrl());
//        user.setOpenId(wxUserInfo.getOpenid());
//        user.setSex(wxUserInfo.getSex());
//        user.setId(uid);
//        return user;
//
//    }

    public static UserInfoResp buildUserInfoResp(User user, Integer modifyNameChance) {
        UserInfoResp userInfoResp = new UserInfoResp();
        BeanUtils.copyProperties(user, userInfoResp);
        userInfoResp.setModifyNameChance(modifyNameChance);
        return userInfoResp;
    }

    public static List<BadgeResp> buildBadgesInfoResp(List<ItemConfig> itemConfigList, List<UserBackpack> backpackList, User user) {
        Set<Long> itemIds = backpackList.stream().map(item -> item.getItemId()).collect(Collectors.toSet());
        return itemConfigList.stream().map(itemConfig -> {
            BadgeResp badgeResp = new BadgeResp();
            BeanUtils.copyProperties(itemConfig, badgeResp);
            badgeResp.setObtain(itemIds.contains(itemConfig.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            badgeResp.setWearing(itemConfig.getId().equals(user.getItemId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            return badgeResp;
        }).sorted(Comparator.comparing(BadgeResp::getObtain, Comparator.reverseOrder())
                .thenComparing(BadgeResp::getWearing, Comparator.reverseOrder()))
                .collect(Collectors.toList());
    }
}
